// run: $exec < input

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <algorithm>
#include <stack>
#include <string>
using namespace std;
struct Node
{
    int val; // if type != 4: number, else 0: +  1: -  2: *
    string strVal;
    int type; // 0: number  1: bool  2: string  4: operator
    int dimen; // dimension of array
    bool isEmpty; // empty array or empty string
};
char s[1005];
void print(Node node)
{
    cout << "Node: val=" << node.val << " str=" << node.strVal << " type=" << node.type << " dimen=" << node.dimen << " empty=" << node.isEmpty << endl;
}
Node makeNode(int val, string strVal, int type, int dimen, bool isEmpty)
{
    Node node;
    node.val = val;
    node.strVal = strVal;
    node.type = type;
    node.dimen = dimen;
    node.isEmpty = isEmpty;
    return node;
}
int getOpVal(char op)
{
    switch (op)
    {
        case '+': return 0;
        case '-': return 1;
        case '*': return 2;
        default: return -1;
    }
}
string getOpValStr(char op)
{
    switch (op)
    {
        case '+': return "+";
        case '-': return "-";
        case '*': return "*";
        default: return "";
    }
}
int cmpOp(char op1, char op2)
{
    int opVal1 = (getOpVal(op1) == 2 ? 2 : 0);
    int opVal2 = (getOpVal(op2) == 2 ? 2 : 0);
    return opVal1 - opVal2;
}
string itoa(int n)
{
    string str;
    char ss[15];
    int len = 0;
    memset(ss, 0, sizeof(ss));
    int neg = 0;
    if (n == 0) ss[0] = '0';
    if (n < 0)
    {
        neg = 1;
        n = -n;
    }
    while (n)
    {
        ss[len++] = n % 10 + '0';
        n /= 10;
    }

    if (neg)
    {
        ss[len] = '-';
        len++;
    }
    for (int i = 0; i < len / 2; i++) swap(ss[i], ss[len - 1 - i]);
    str = ss;
    //cout << "itoa " << str << endl;
    return str;
}
string getString(Node a, Node b)
{
    string sa, sb;
    if (a.type == 2) sa = a.strVal;
    else if (a.dimen && a.isEmpty) sa = "";
    else sa = itoa(a.val);
    if (b.type == 2) sb = b.strVal;
    else if (b.dimen && b.isEmpty) sb = "";
    else sb = itoa(b.val);
    return sa + sb;
}
int atoi(string s)
{
    int neg = 0, res = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '-') neg ^= 1;
        if (s[i] >= '0' && s[i] <= '9')
        {
            res *= 10;
            res += s[i] - '0';
        }
    }
    return neg ? -res : res;
}
pair<int, int> getNodeSpan(char s[], int &cur, int end)
{
    pair<int, int> res;
    res.first = cur;
    int leftBracketNumber = 0;
    bool flag = false;
    while (true)
    {
        if (s[cur] == '[')
        {
            leftBracketNumber++;
            flag = true;
        }
        if (s[cur] == ']') leftBracketNumber--;
        cur++;
        if (leftBracketNumber != 0) continue;
        if (!flag) continue;
        if (cur != end && s[cur] == '[') continue;
        break;
    }
    res.second = cur;
    return res;
}
Node calcExpr(Node node1, Node node2, Node nodeOp)
{
    Node res;
    //+
    if (nodeOp.val == 0)
    {
        // bool or int && !array
        if ((node1.type == 0 || node1.type == 1)
            && (node2.type == 0 || node2.type == 1)
            && node1.dimen == 0
            && node2.dimen == 0)
        {
            res = makeNode(node1.val + node2.val, itoa(node1.val + node2.val), 0, 0, false);
        }
        // string or array
        else
        {
            string strVal = getString(node1, node2);
            res = makeNode(atoi(strVal), strVal, 2, 0, strVal == "");
            //cout << res.strVal << " a " << res.val << endl;
        }
    }
    //-
    if (nodeOp.val == 1)
    {
        res = makeNode(node1.val - node2.val, itoa(node1.val - node2.val), 0, 0, false);
    }
    //*
    if (nodeOp.val == 2)
    {
        res = makeNode(node1.val * node2.val, itoa(node1.val * node2.val), 0, 0, false);
    }
    return res;
}
Node calc(char s[], int start, int end);
int height[1005];
Node getElement(char s[], int &cur, int end)
{
    string sss = s;
    //cout << "getElement: start=" << cur << " end=" << end << " expr: " << sss.substr(cur, end - cur) << endl;
    int leftBracketPos = cur;
    int rightBracketPos;
    int bracketNumber = 1;
    cur++;
    while (bracketNumber)
    {
        if (s[cur] == '[') bracketNumber++;
        if (s[cur] == ']') bracketNumber--;
        cur++;
    }
    rightBracketPos = cur - 1;
    Node node = calc(s, leftBracketPos + 1, rightBracketPos);
    node.dimen++;
    return node;
}
Node simplify(char s[], int start, int end)
{
    string sss = s;
    //cout << "simplify: start=" << start << " end=" << end << " expr: " << sss.substr(start, end - start) << endl;
    stack<char> uOpStack;
    int cur = start;
    while (s[cur] != '[') uOpStack.push(s[cur++]);
    // find first [...], the next [...]'s are index
    Node node = getElement(s, cur, end);
    while (cur != end)
    {
        Node index = getElement(s, cur, end);
        // array[0] => dimen--;
        if (node.dimen > 0) node.dimen--;
        // string[index]
        else
        {
            // node is a string
            node.strVal = node.strVal[index.val];
            node.val = node.strVal[0] - '0';
            node.isEmpty = false;
        }
    }
    //cout << "ustack" << endl;
    while (!uOpStack.empty())
    {
        char op = uOpStack.top();
        //cout << op << ": ";
        //print(node);
        uOpStack.pop();
        if (op == '-')
        {
            node.val = -node.val;
            node.strVal = itoa(node.val);
            node.type = 0;
            node.dimen = 0;
            node.isEmpty = false;
        }
        if (op == '+')
        {
            node.type = 0;
            node.dimen = 0;
            node.isEmpty = false;
        }
        if (op == '!')
        {
            // !array -> false
            if (node.dimen)
            {
                node.val = 0;
            }
            // !string(not empty) -> false
            else if (node.strVal != "" && node.type == 2)
            {
                node.val = 0;
            }
            // !string(empty) -> true
            else if (node.strVal == "" && node.type == 2)
            {
                node.val = 1;
            }
            // false -> true, true -> false
            else if (node.type == 1)
            {
                node.val ^= 1;
            }
            // int !0 -> 0
            else if (node.type == 0 && node.val != 0)
            {
                node.val = 0;
            }
            // int 0 -> 1
            else if (node.type == 0 && node.val == 0)
            {
                node.val = 1;
            }
            node.strVal = node.val ? "true" : "false";
            node.type = 1;
            node.dimen = 0;
            node.isEmpty = false;
        }
    }
    //print(node);
    return node;
}
Node calc(char s[], int start, int end)
{
    string sss = s;
    //cout << "calc: start=" << start << " end=" << end << " expr: " << sss.substr(start, end - start) << endl;
    if (start == end)
    {
        Node node = makeNode(0, "0", 0, 0, true);
        return node;
    }
    stack<Node> nodeStack;
    stack<char> opStack;
    int cur = start;
    // make suffix expression
    while (cur < end)
    {
        pair<int, int> p = getNodeSpan(s, cur, end);
        // simplify arrays such as [["100"]][0][0][1][0] => "0"
        nodeStack.push(simplify(s, p.first, p.second));
        if (cur < end)
        {
            char op = s[cur++];
            if (opStack.empty() || cmpOp(op, opStack.top()) > 0) opStack.push(op);
            else
            {
                while (!opStack.empty() && cmpOp(op, opStack.top()) <= 0)
                {
                    Node node = makeNode(getOpVal(opStack.top()), getOpValStr(opStack.top()), 4, 0, false);
                    nodeStack.push(node);
                    //cout << opStack.top() << endl;
                    //print(node);
                    opStack.pop();
                }
                opStack.push(op);
            }
        }
    }
    while (!opStack.empty())
    {
        Node node = makeNode(getOpVal(opStack.top()), getOpValStr(opStack.top()), 4, 0, false);
        nodeStack.push(node);
        //cout << opStack.top() << endl;
        //print(node);
        opStack.pop();
    }
    // calc suffix expression
    //cout << "tmpstack:" << endl;
    stack<Node> tmpStack;
    while (!nodeStack.empty())
    {
        //cout << "tmpstack ";
        //print(nodeStack.top());
        tmpStack.push(nodeStack.top());
        nodeStack.pop();
    }
    stack<Node> resultStack;
    while (!tmpStack.empty())
    {
        Node node = tmpStack.top();
        tmpStack.pop();
        if (node.type == 4)
        {
            Node node2 = resultStack.top();
            resultStack.pop();
            Node node1 = resultStack.top();
            resultStack.pop();
            //cout << "1: " << endl << "node1: ";
            //print(node1);
            //cout << "node2: ";
            //print(node2);
            //cout << "node: ";
            //print(node);
            //cout << "calc: ";
            //print(calcExpr(node1, node2, node));
            resultStack.push(calcExpr(node1, node2, node));
        }
        else
        {
            resultStack.push(node);
            //cout << "2: ";
            //print(node);
        }
    }
    return resultStack.top();
}
void testCalcExpr()
{
    Node node1 = makeNode(1, "1", 2, 0, false);
    Node node2 = makeNode(5, "5", 2, 0, false);
    Node node = makeNode(0, "+", 4, 0, false);
    print(calcExpr(node1, node2, node));
    exit(0);
}
void testItoa()
{
    itoa(0);
    itoa(1);
    itoa(15);
    itoa(20);
    itoa(100);
    itoa(-0);
    itoa(-1);
    itoa(-15);
    itoa(-20);
    itoa(-100);
    exit(0);
}
void testCmpOp()
{
    cout << cmpOp('+', '-') << endl;
    cout << cmpOp('-', '+') << endl;
    cout << cmpOp('+', '*') << endl;
    cout << cmpOp('*', '-') << endl;
    cout << cmpOp('*', '*') << endl;
    exit(0);
}
int main()
{
    //testCalcExpr();
    //testItoa();
    //testCmpOp();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%s", s);
        printf("%d\n", calc(s, 0, strlen(s)).val);
    }
    return 0;
}
